


from flask import Blueprint
from flask import request
from flask import jsonify
import psycopg2.extras
from database import db_pool_singleton
from database.user_role_relation_table_method import UserRoleRelationTableMethod
from database.user_role_table_method import UserRoleTableMethod


role_blueprint = Blueprint('role', __name__)

'''
-- 用户角色表    
CREATE TABLE IF NOT EXISTS user_role (    
    role_id SERIAL PRIMARY KEY,    
    role_name VARCHAR(20) NOT NULL,    
    role_desc VARCHAR(100) NOT NULL    
);
-- 用户角色关联表    
CREATE TABLE IF NOT EXISTS user_role_relation (    
    user_id INTEGER NOT NULL REFERENCES user_info(user_id),    
    role_id INTEGER NOT NULL REFERENCES user_role(role_id),    
    PRIMARY KEY (user_id, role_id)    
);

'''

@role_blueprint.route('/insert_role', methods=['POST'])
def insert_role():
    role_name = request.form.get('role_name')
    role_desc = request.form.get('role_desc')
    if not role_name or not role_desc:
        args_error = {
            "status": -1,
            "errorCode": 3001,
            "errorMsg": "args error",
            "resultBody": {}
        }
        return jsonify(args_error)
    
    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_role_table_method = UserRoleTableMethod(conn=conn, cur=cur)

        if user_role_table_method.get_user_role_by_name(role_name):

            args_error = {
                "status": -1,
                "errorCode": 3002,
                "errorMsg": "role name already exists",
                "resultBody": {}
            }
            return jsonify(args_error)

        role_id = user_role_table_method.insert_user_role(role_name, role_desc)

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {
                "role_id": role_id,
                "role_name": role_name,
                "role_desc": role_desc
            }
        }

        return jsonify(args_success)
    

@role_blueprint.route('/get_all_user_role', methods=['GET'])
def get_all_user_role():

    limit = request.args.get('limit', default=10, type=int)
    
    page = request.args.get('page', default=1, type=int)


    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)


        user_role_table_method = UserRoleTableMethod(conn=conn, cur=cur)


        user_role_list = user_role_table_method.get_all_user_role(limit=limit, page=page)

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {
                "user_role_list": user_role_list
            }
        }


        return jsonify(args_success)


@role_blueprint.route('/get_user_role_by_id', methods=['GET'])
def get_user_role_by_id():
    role_id = request.args.get('role_id')


    if not role_id:

        args_error = {
            "status": -1,
            "errorCode": 3003,
            "errorMsg": "args error",
            "resultBody": {}
        }

        return jsonify(args_error)

    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_role_table_method = UserRoleTableMethod(conn=conn, cur=cur)

        user_role = user_role_table_method.get_user_role_by_id(role_id)

        if not user_role:

            args_error = {
                "status": -1,
                "errorCode": 3004,
                "errorMsg": "role id not exists",
                "resultBody": {}
            }

            return jsonify(args_error)
        

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {
                "user_role": user_role
            }
        }

        return jsonify(args_success)
    

@role_blueprint.route('/delete_user_role_by_id', methods=['POST'])
def delete_user_role_by_id():
    role_id = request.form.get('role_id')
    

    if not role_id:

        args_error = {
            "status": -1,
            "errorCode": 3005,
            "errorMsg": "args error",
            "resultBody": {}
        }

        return jsonify(args_error)

    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_role_table_method = UserRoleTableMethod(conn=conn, cur=cur)

        if not user_role_table_method.delete_user_role_by_id(role_id):

            args_error = {
                "status": -1,
                "errorCode": 3006,
                "errorMsg": "delete role failed",
                "resultBody": {}
            }

            return jsonify(args_error)

        args_success = {

            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": "delete role success"
        }

        return jsonify(args_success)
    
# 用户角色关联表    
@role_blueprint.route('/insert_user_role_relation', methods=['POST'])
def insert_user_role_relation():
    user_id = request.form.get('user_id')
    role_id = request.form.get('role_id')

    if not user_id or not role_id:

        args_error = {
            "status": -1,
            "errorCode": 3007,
            "errorMsg": "args error",
            "resultBody": {}
        }

        return jsonify(args_error)

    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_role_relation_table_method = UserRoleRelationTableMethod(conn=conn, cur=cur)

        if user_role_relation_table_method.select_user_role_relation_by_user_id_and_role_id(user_id, role_id):

            args_success = {
                "status": 1,
                "errorCode": None, 
                "errorMsg": None,
                "resultBody": "user id and role id already exists"
            }
            return jsonify(args_success)
        

        user_role_relation_table_method.insert_user_role_relation(user_id, role_id)

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": "insert user role relation success"
        }

        return jsonify(args_success)

# 用户角色解除关联    
@role_blueprint.route('/delete_user_role_relation', methods=['POST'])
def delete_user_role_relation():
    user_id = request.form.get('user_id')
    role_id = request.form.get('role_id')

    if not user_id or not role_id:

        args_error = {
            "status": -1,
            "errorCode": 3008,
            "errorMsg": "args error",
            "resultBody": {}
        }

        return jsonify(args_error)

    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_role_relation_table_method = UserRoleRelationTableMethod(conn=conn, cur=cur)

        user_role_relation_table_method.delete_user_role_relation(user_id, role_id)


        args_success = {
            "status": 1,            
            "errorCode": None, 
            "errorMsg": None,
            "resultBody": "delete user role relation success"
        }


        return jsonify(args_success)



@role_blueprint.route('/select_user_role_relation', methods=['GET'])
def select_user_role_relation():

    user_id = request.args.get('user_id')


    if not user_id:

        args_error = {
            "status": -1,
            "errorCode": 3009,
            "errorMsg": "args error",
            "resultBody": {}
        }   

   
        return jsonify(args_error)
    

    with db_pool_singleton.getconn() as conn:
        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        user_role_relation_table_method = UserRoleRelationTableMethod(conn=conn, cur=cur)

        user_role_relation_list = user_role_relation_table_method.select_user_role_relation(user_id)

        if not user_role_relation_list:

            args_error = {
                "status": -1,
                "errorCode": 3010,
                "errorMsg": "user id not exists",
                "resultBody": {}
            }

            return jsonify(args_error)

        args_success = {
            "status": 1,
            "errorCode": None,
            "errorMsg": None,
            "resultBody": {
                "user_role_relation_list": user_role_relation_list
            }

        }


        return jsonify(args_success)